package cn.appsys.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class MyCustomDateConverter implements Converter<String, Date>{

	private String[] dateFormats;
	public void setDateFormats(String[] dateFormats) {
		this.dateFormats = dateFormats;
	}
	public Date convert(String value) {
		if(value == null || value.isEmpty())
			return null;
		else {
			for (String string : dateFormats) {
				SimpleDateFormat sdf = new SimpleDateFormat(string);
				try {
					return sdf.parse(value);
				} catch (ParseException e) {
					continue;
				}
			}
			throw new RuntimeException("日期格式不正确");
		}
		
	}
	
}
